package com.chaos.meta.common;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class CommonResult<T>
{
	private String resultCode = "-1";
	
	private String resultMessage = null;

	private Integer resultCount = 0;
	
	private T resultData = null;

	public static final String LOGIN_CODE = "-2";
	public static final String LOGIN_MESSAGE = "Login please !";
	
	public CommonResult(){
		resultMessage = "SUCCESS";
		resultCode = "0";
	}
	
	/**
	 *  construct a successful result with array data
	 */
	public CommonResult(T obj){
		resultMessage = "SUCCESS";
		resultCode = "0";
		resultData = obj;
	}
	
	/**
	 *  construct a failure result with a error code
	 * @param errorCode could be "0" then errorMessage is a successful message
	 * @param errorMessage
	 */
	public CommonResult(String errorCode, String errorMessage)
	{
		resultCode = errorCode;
		resultMessage = (String)errorMessage;
	}

	public CommonResult(String resultCode, String errorMessage, T resultData)
	{
		this.resultCode = resultCode;
		this.resultMessage = errorMessage;
		this.resultData = resultData;
	}

	/**
	 *  construct a failure result with default error code -1
	 * @param failedMessage
	 */
	public CommonResult(String failedMessage)
	{
		resultMessage = failedMessage;
	}

	/**
	 *  construct a failure result with some exception message and with default error code -1
	 * @param e
	 */
	public CommonResult(Exception e)
	{
		String eStr = e.getMessage();
		if(eStr == null) {
			eStr = "NullPointerException";
		}else {
			Throwable t=e.getCause();
			int i = 0;
			while(t != null && i < 3) {
				eStr += '\n';
				eStr += t.getMessage();
				t = t.getCause();
				i++;
			}
		}
		resultMessage = eStr;
	}

	public static CommonResult buildSuccessResult(Object data, Integer arraySize)
	{
		CommonResult successResult = new CommonResult();
		successResult.setResultMessage("SUCCESS");
		successResult.setResultCount(arraySize);
		successResult.setResultCode("0");
		successResult.setResultData(data);
		return successResult;
	}

	public static CommonResult buildSuccessResult(Object data)
	{
		CommonResult successResult = new CommonResult();
		successResult.setResultMessage("SUCCESS");
		successResult.setResultCount(1);
		successResult.setResultCode("0");
		successResult.setResultData(data);
		return successResult;
	}

	public static CommonResult buildFailedResult(String resultCode, String failedMessage)
	{
		CommonResult failedResult = new CommonResult();
		failedResult.setResultMessage(failedMessage);
		failedResult.setResultCode(resultCode);
		return failedResult;
	}

	public static CommonResult buildFailedResult(String failedMessage)
	{
		CommonResult failedResult = new CommonResult();
		failedResult.setResultMessage(failedMessage);
		failedResult.setResultCode("-1");
		return failedResult;
	}

	public static CommonResult buildRunningResult()
	{
		CommonResult runningResult = new CommonResult();
		runningResult.setResultCode("1");
		return runningResult;
	}

	public static CommonResult buildSuccessResult()
	{
		CommonResult successResult = new CommonResult();
		successResult.setResultMessage("SUCCESS");
		successResult.setResultCount(1);
		successResult.setResultCode("0");
		successResult.setResultData(null);
		return successResult;
	}

	public static CommonResult buildFailedResult()
	{
		CommonResult failedResult = new CommonResult();
		failedResult.setResultMessage("Interval error, please contact the administrator!");
		failedResult.setResultCode("-1");
		return failedResult;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getResultMessage() {
		return resultMessage;
	}

	public void setResultMessage(String resultMessage) {
		this.resultMessage = resultMessage;
	}

	public Integer getResultCount() {
		return resultCount;
	}

	public void setResultCount(Integer resultCount) {
		this.resultCount = resultCount;
	}

	public T getResultData() {
		return resultData;
	}

	public void setResultData(T resultData) {
		this.resultData = resultData;
	}
}
